package com.shanzhe.eshop.auth.controller;

import com.shanzhe.eshop.auth.pojo.AuthPriorityDTO;
import com.shanzhe.eshop.auth.pojo.AuthPriorityVO;
import com.shanzhe.eshop.auth.service.AuthPriorityService;
import com.shanzhe.eshop.common.constant.UrlConstant;
import com.shanzhe.eshop.common.util.ResultBean;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author yinwei
 * @create 2021/10/2
 * @Version 1.0
 */
@RestController
@Api(value = "权限管理模块")
@RequestMapping(value = UrlConstant.AUTH_URL)
public class AuthPriorityController {

    @Autowired
    AuthPriorityService authPriorityService;

    /**
     * 查询权限列表
     * @return List<AuthPriorityDO> 权限集合
     */
    @GetMapping("/priority/root")
    ResultBean<List<AuthPriorityVO>> listRootPriorities(){
        ResultBean<List<AuthPriorityDTO>> resultBean = authPriorityService.listPriorityies();
         ArrayList<AuthPriorityVO> authPriorityVOS = new ArrayList<AuthPriorityVO>(resultBean.getData().size());
         for(AuthPriorityDTO authPriorityDTO:resultBean.getData()){
             authPriorityVOS.add(authPriorityDTO.clone(AuthPriorityVO.class));
         }

        return ResultBean.success(authPriorityVOS);
    }

    /**
     * 根据父权限id查询子权限
     * @param parentId
     * @return
     */
    @GetMapping("/child/{parentId}")
    ResultBean<List<AuthPriorityVO>> listChildPriorities(@PathVariable("parentId") Long parentId){
        ResultBean<List<AuthPriorityDTO>> resultBean = authPriorityService.listChildPriorities(parentId);

        ArrayList<AuthPriorityVO> authPriorityVOS = new ArrayList<AuthPriorityVO>(resultBean.getData().size());
        for(AuthPriorityDTO authPriorityDTO:resultBean.getData()){
            authPriorityVOS.add(authPriorityDTO.clone(AuthPriorityVO.class));
        }

        return ResultBean.success(authPriorityVOS);
    }
}
